﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class Users_CancleTicket : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string userid = Membership.GetUser().ProviderUserKey.ToString();
            int ticketid = Convert.ToInt32(Request.QueryString["id"]);
            TicketBAL _ticketBAL = new TicketBAL();
            if (_ticketBAL.checkTicket(userid, ticketid, "B") == true)
            {
                int price = _ticketBAL.cancelTicket(ticketid);
                if (price > 0)
                {
                    Ticket ticket = _ticketBAL.getByID(ticketid);
                    int discount = _ticketBAL.getDiscount(ticket.Flight.DepartTime, "Cancel");
                    int refund = Convert.ToInt32(price * discount / 100);

                    FlightBAL _flightBAL = new FlightBAL();
                    double distance = _flightBAL.calDistance(_flightBAL.GetLocationByID(ticket.Flight.Route.Origin.ID),_flightBAL.GetLocationByID(ticket.Flight.Route.Destination.ID));

                    ProfileCommon p = Profile.GetProfile(Membership.GetUser().UserName);
                    p.SkyMeter -= distance;
                    p.Save();

                    Label1.Text = "Cancel Completed.<br />Total refund : " + refund.ToString()+" USD";
                }
                else if (price == 0)
                {
                    Label1.Text = "Cancel Completed.";
                }
                else
                {
                    Label1.Text = "Cancel Incompleted.<br />Please check ticket status again";
                }
            }
            else
            {
                Label1.Text = "You can't cancel this ticket.";
            }
        }
        catch (Exception)
        {
            Label1.Text = "You can't cancel this ticket.";            
        }
    }
}
